package com.hc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hc.common.bean.PageBean;
import com.hc.domain.Comment;
import com.hc.domain.Comment2;
import com.hc.domain.User;
import com.hc.mapper.Comment2Mapper;
import com.hc.res.Comment2Item;
import com.hc.res.UserComment2;
import com.hc.service.Comment2Service;
import com.hc.service.CommentService;
import com.hc.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hc
 * @date 2021/12/27 10:30
 */
@Service
public class Comment2ServiceImpl extends ServiceImpl<Comment2Mapper, Comment2> implements Comment2Service {

    @Resource
    private CommentService commentService;

    @Override
    public boolean publisComment2(Comment2 comment2) {
        //将评论添加到tb_comment2表中
        baseMapper.insert(comment2);
        //将表tb_comment的comments数量+1
        Comment comment = commentService.getById(comment2.getCommentId());
        comment.setComments(comment.getComments() + 1);
        commentService.updateById(comment);
        return true;
    }

    @Resource
    private UserService userService;

    @Override
    public PageBean<UserComment2> getUserComment2ByCommentId(Long commentId, Integer pageNum, Integer pageSize) {
        Page<Comment2> page = new Page<>(pageNum, pageSize);
        //求出对commentId回复的用户的编号集合
        baseMapper.selectPage(page, new QueryWrapper<Comment2>()
                .select("user_id")
                .eq("comment_id", commentId));

        List<Long> userIdList = page.getRecords()
                .stream().map(item -> item.getUserId()).distinct().collect(Collectors.toList());
        List<UserComment2> userComment2List = new ArrayList<>();
        for (Long userId : userIdList) {
            UserComment2 userComment2 = new UserComment2();
            User user = userService.getById(userId);
            userComment2.setId(user.getId());
            userComment2.setAvatar(user.getAvatar());
            userComment2.setName(user.getNickname());

            List<Comment2Item> list = new ArrayList<>();
            List<Comment2> comment2List = baseMapper.selectList(new QueryWrapper<Comment2>().eq("user_id", userId));
            for (Comment2 comment2 : comment2List) {
                Comment2Item tmp = new Comment2Item(comment2.getContent(), comment2.getCreateTime());
                list.add(tmp);
            }
            userComment2.setComment2ItemList(list);
            userComment2List.add(userComment2);
        }

        PageBean<UserComment2> pageBean = new PageBean<>();
        pageBean.setPageSize((int) page.getSize());
        pageBean.setCurrent(page.getCurrent());
        pageBean.setPages(page.getPages());
        pageBean.setRecords(userComment2List);

        return pageBean;
    }
}
